package com.lin.cms.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Channel implements Serializable {

	private static final long serialVersionUID = 1L;

	private int channelId;

	private int modelId;

	private int siteId;

	private int parentId;

	private String channelPath;

	private int lft;

	private int rgt;

	private int priority;

	private Boolean hasContent;

	private Boolean isDisplay;
	
	private List<Channel> child;
	
	private Model model;
	
	private ChannelExt channelExt;
	
	private Channel parent;
	
	private List<Groups> viewGroups;
	
	private List<Groups> contriGroups;
	
	private List<CmsUser> users;
	
	private Site site;
	

	public Site getSite() {
		return site;
	}

	public void setSite(Site site) {
		this.site = site;
	}

	public Channel() {
	}
	
	public Model getModel() {
		return model;
	}

	public void setModel(Model model) {
		this.model = model;
	}

	public int getChannelId() {
		return channelId;
	}
	
	public List<CmsUser> getUsers() {
		return users;
	}

	public void setUsers(List<CmsUser> users) {
		this.users = users;
	}

	public List<Groups> getViewGroups() {
		return viewGroups;
	}

	public void setViewGroups(List<Groups> viewGroups) {
		this.viewGroups = viewGroups;
	}

	public void setChannelId(int channelId) {
		this.channelId = channelId;
	}
	
	public List<Channel> getChild() {
		return child;
	}

	public void setChild(List<Channel> child) {
		this.child = child;
	}
	
	public List<Groups> getContriGroups() {
		return contriGroups;
	}

	public void setContriGroups(List<Groups> contriGroups) {
		this.contriGroups = contriGroups;
	}

	public ChannelExt getChannelExt() {
		return channelExt;
	}
	public void setChannelExt(ChannelExt channelExt) {
		this.channelExt = channelExt;
	}
	public int getModelId() {
		return modelId;
	}

	public void setModelId(int modelId) {
		this.modelId = modelId;
	}

	public int getSiteId() {
		return siteId;
	}

	public void setSiteId(int siteId) {
		this.siteId = siteId;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public String getChannelPath() {
		return channelPath;
	}

	public void setChannelPath(String channelPath) {
		this.channelPath = channelPath;
	}

	public int getLft() {
		return lft;
	}

	public void setLft(int lft) {
		this.lft = lft;
	}

	public int getRgt() {
		return rgt;
	}

	public void setRgt(int rgt) {
		this.rgt = rgt;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}
	

	public Boolean getHasContent() {
		return hasContent;
	}

	public void setHasContent(Boolean hasContent) {
		this.hasContent = hasContent;
	}

	public Boolean getIsDisplay() {
		return isDisplay;
	}

	public void setIsDisplay(Boolean isDisplay) {
		this.isDisplay = isDisplay;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + channelId;

		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Channel other = (Channel) obj;
		if (channelId != other.channelId)
			return false;

		return true;
	}
	
	@Override
	public String toString() {
		return getClass().getName() + "@" + Integer.toHexString(hashCode()) + 
			"(" + 
			"channelId=" + "'" + channelId + "'" + 
			")";
	}
	
	public String getTplChannel() {
		ChannelExt ext = getChannelExt();
		if (ext != null) {
			return ext.getTplChannel();
		} else {
			return null;
		}
	}
	
	public String getTplContent() {
		ChannelExt ext = getChannelExt();
		if (ext != null) {
			return ext.getTplContent();
		} else {
			return null;
		}
	}

	public Channel getParent() {
		return parent;
	}

	public void setParent(Channel parent) {
		this.parent = parent;
	}
	
	/**
	 * 递归将子栏目加入列表。条件：有内容的栏目。
	 * 
	 * @param list
	 *            栏目容器
	 * @param channel
	 *            待添加的栏目，且递归添加子栏目
	 * @param rights
	 *            有权限的栏目，为null不控制权限。
	 */
	private static void addChildToList(List<Channel> list, Channel channel,
			Set<Channel> rights, Channel exclude, boolean hasContentOnly) {
		if ((rights != null && !rights.contains(channel))
				|| (exclude != null && exclude.equals(channel))) {
			return;
		}
		list.add(channel);
		List<Channel> child = channel.getChild();
		for (Channel c : child) {
			if (hasContentOnly) {
				addChildToList(list, c, rights, exclude, hasContentOnly);
			} else {
				addChildToList(list, c, rights, exclude, hasContentOnly);
			}
		}
	}
	
	public static List<Channel> getListForSelect(List<Channel> topList,
			Set<Channel> rights, Channel exclude, boolean hasContentOnly) {
		List<Channel> list = new ArrayList<Channel>();
		for (Channel c : topList) {
			addChildToList(list, c, rights, exclude, hasContentOnly);
		}
		return list;
	}
	
	
	public Integer[] getUserIds() {
		List<CmsUser> users = getUsers();
		return CmsUser.fetchIds(users);
	}
	
	
	
}
